
<template>
  <el-dialog
    :close-on-click-modal="false"
    title="文件夹编辑"
    :visible.sync="show"
    width="700px"
    center
  >
    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="100px">
      <el-form-item label="编号" prop="number">
        <el-input v-model="form.number" :disabled="disableForm" />
      </el-form-item>
      <el-form-item label="文档名称" prop="name">
        <el-input v-model="form.name" :disabled="disableForm" />
      </el-form-item>
      <el-form-item label="关键字">
        <el-input v-model="form.keyword" :disabled="disableForm" />
      </el-form-item>
      <el-form-item label="文档类型">
        <el-select v-model="form.type" :disabled="disableForm" style="width: 100%;" placeholder="请选择文档类型">
          <el-option v-for="item in docTypes" :key="item.name" :label="item.name" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="文档状态">
        <el-radio-group v-model="form.status" :disabled="disableForm">
          <el-radio v-for="item in docStatus" :key="item.name" :label="item.value">
            {{ item.name }}
          </el-radio></el-radio-group>
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="form.description" :disabled="disableForm" type="textarea" />
      </el-form-item>
      <!-- <el-form-item label="文件上传">
        <uploader v-model="fileList" :disabled="disableForm" :can-download="optType==='detail'" />
      </el-form-item> -->
      <div class="com-form-title">附件</div>
      <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
      <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" @download="download" @preview="preview" />
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="saveDoc">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addDocumentDetail, updateDocumentDetail, getDocumentDetailDetail, addCompanyDocumentOperate } from '@/api/knowledgeBase'
import { docStatus, docTypes } from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'
export default {
  name: 'DirManage',
  components: {
    fileUploader
  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    dir: {
      type: Object,
      default() {
        return {}
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      showFileModal: false,
      docTypes,
      docStatus,
      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        name: '',
        type: 1,
        status: 1,
        keyword: '',
        description: '',
        number: ''
      },
      fileList: [
      ],
      rules: {
        name: [
          { required: true, message: '请输入文档名称', trigger: 'change' }
        ],
        number: [
          { required: true, message: '请输入文档编码', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    // data: {
    //   handler(newData, oldData) {
    //     if (newData.id) {
    //       this.getDocDetail(newData.id)
    //       Object.keys(this.form).forEach(key => {
    //         this.form[key] = newData[key]
    //       })
    //     }
    //   },
    //   deep: true,
    //   immediate: true
    // },
    showModal(val) {
      if (val && this.optType !== 'add') {
        this.getDocDetail(this.data.id)
        this.$nextTick(() => {
          Object.keys(this.form).forEach(key => {
            this.form[key] = this.data[key]
          })
        })
      } else if (!val) {
        this.fileList = []
        this.$refs.form.resetFields()
      }
    }
  },
  methods: {
    download(data) {
      addCompanyDocumentOperate({ operateFileName: data.name, operateFileType: 3 }).then(res => {

      })
    },
    preview(data) {
      addCompanyDocumentOperate({ operateFileName: data.name, operateFileType: 4 }).then(res => {

      })
    },
    getDocDetail(id) {
      getDocumentDetailDetail({ companyDocumentDetailId: id }).then(res => {
        const { lstAttachment } = res.data || {}
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
      })
    },
    saveDoc() {
      if (this.optType === 'detail') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = {
            companyDocumentDetail: {
              ...this.form,
              companyDocumentDirectoryId: this.dir.id,
              ...this.optType === 'edit' ? {
                id: this.data.id
              } : {}
            },
            lstAttachment: this.fileList
          }
          const fn = this.optType === 'add' ? addDocumentDetail : updateDocumentDetail
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
